<?php 
	class General_model extends CI_Model
	{
		private $table = "no_news";
				
		/*
		*
		*	Tinh tổng số bài viết hiện có
		*
		*/
		public function admin_count($id = 1)
		{
			$this->db->select('*');
			$this->db->where('parent_id',$id);
			$query = $this->db->get($this->table);
			
			$result = $query->result_array();
			$query->free_result();
			
			if(!empty($result))
			{
				return count($result);
			}else{
				return false;
			}
		}		
		
		public function admin_save($image_thumb = '')
		{
			$id = intval($this->input->post('hidden_id'));
			if($this->input->post('status')=='on'){
				$status = 1;
			}else{
				$status = 0;
			}
			
			if($id == 0)
			{
				$data_content = array( 
					'parent_id' 	=> $this->input->post('parent_id'),
					'name' 			=> $this->input->post('name'),
					'thumb'			=> $image_thumb,
					'slug' 			=> $this->input->post('slug'),
					'intro'			=> $this->input->post('intro'),
					'content'		=> $this->input->post('content'),
					'view_count'	=> $this->input->post('view_count'),
					'author'	=> $this->input->post('author'),
					'created_time' 	=> getNow(),
					'modified_time' => getNow(),
					'order' 		=> $this->input->post('order'),
					'status' 		=> $status
				);
			
				if($this->db->insert($this->table,$data_content)){
					//$this->update_count($id);
					return true;
				}else{
					return false;
				}
			}else{
				$image_mapping = $this->get_image_product($id);
				if($image_thumb == '')
				{
					$image_thumb = $image_mapping[0]['thumb'];
				}else{
					@unlink(base_url().IMAGE_LARGE_DIR.$image_mapping[0]['thumb']);
				}
				$data_content = array( 
					'parent_id' 	=> $this->input->post('parent_id'),
					'name' 			=> $this->input->post('name'),
					'thumb'			=> $image_thumb,
					'slug' 			=> $this->input->post('slug'),
					'intro'			=> $this->input->post('intro'),
					'content'		=> $this->input->post('content'),
					'view_count'	=> $this->input->post('view_count'),
					'author'	=> $this->input->post('author'),
					'modified_time' => getNow(),
					'order' 		=> $this->input->post('order'),
					'status' 		=> $status
				);
				$this->db->where('id',$id);
				if($this->db->update($this->table,$data_content)){
					return true;
				}else{
					return false;
				}
			}
		}
		
		/*
		*	
		*	Load bài viết với start và limit
		*
		*/
		public function admin_index($start = 0,$limit = 10)
		{
			$this->db->select('*');
			$this->db->order_by('order','DESC');
			$this->db->limit($start,$limit);
			$query = $this->db->get($this->table);
			
			$result = $query->result_array();
			$query->free_result();
			if(!empty($result))
			{
				return $result;
			}else{
				return false;
			}
		}
		
		/*
		*
		*	Load bài viết với ID truyền vào
		*
		*/
		public function admin_update($id = 0)
		{
			$id = intval($id);
			$this->db->select('*');		
		
			$this->db->where('id',$id);
						
			$query = $this->db->get($this->table);

			$result = $query->result_array();
			$query->free_result();
			
			if(!empty($result))
			{
				return $result;
			}else{
				return false;
			}			
		}
		
		
		/*
		*
		*	Dùng để xóa một bài viết
		*
		*/
		public function admin_del($product_id = 0)
		{
			$product_id = intval($product_id);

			$this->db->where('id',$product_id);
			if($this->db->delete($this->table))
			{
				// Tinh lai tong so bai viet cua bai do
				//$this->update_count($post_id);
				return true;
			}
			return false;
		}
		
		/*
		*
		*	Dùng để xóa bài viết được chọn
		*
		*/
		public function admin_del_all()
		{
			$value	= array();
			$value  = $this->input->post('check_item');
			foreach($value as $val)
			{
				$this->db->where('id',$val);
				$this->db->delete($this->table);
				$array_del = array();
			}
		}
			
		public function admin_sort($sort)
		{
			$status = '';
			$incre = '';
			
			switch($sort)
			{
				case 1:
					$status = 'product.available_id';
					$incre  = 1;
					break;
				case 2:
					$status = 'product.available_id';
					$incre  = 2;
					break;
				case 3:
					$status = 'product.available_id';
					$incre  = 3;
					break;
				case 4:
					$status = 'product.promotion_id';
					$incre  = 1;
					break;
				case 5:
					$status = 'product.is_bestsale';
					$incre  = 1;
					break;
				case 6:
					$status = 'product.status';
					$incre  = 1;
					break;
				case 7:
					$status = 'product.status';
					$incre  = 0;
					break;
			}
			
			$this->db->select('product.*,categories.name AS cat_name,categories.id AS cat_id,promotion.id AS pro_id,promotion.name AS pro_name,status.name AS avai_name');
			$this->db->from('hts_products AS product');
			$this->db->join('hts_categories AS categories','categories.id = product.category_id','LEFT');
			$this->db->join('hts_promotion AS promotion','promotion.id = product.promotion_id','LEFT');
			$this->db->join('hts_status AS status','status.id = product.available_id','LEFT');
			
			if($sort != 9999)
			{
				$this->db->where($status,$incre);
			}
			$this->db->order_by('created_time','DESC');
			
			$query  = $this->db->get();
			
			$result = $query->result_array();
			$query->free_result();
			
			return $result;
		}
		
		/*
		*
		*	Update trạng thái bài viết.
		*	Cho hiện hoặc ẩn bài viết ngoài trang chủ
		*
		*/
		function admin_update_status($id,$status)
		{
			$id = (int)$id;
			$this->db->where('id',$id);
			$data = array('status'=>$status);
			if($this->db->update($this->table,$data))
			{
				return true;
			}else{
				return false;
			}
		}
		
		public function admin_search()
		{
			$keyword   = $this->input->post('keyword');
			$this->db->select('product.*,cat.id as cat_id,cat.name');
			$this->db->from('hts_products AS product');
			$this->db->join('hts_products_cateogories AS cat','cat.id = product.category_id');
			
			if($keyword != '')
			{
				$this->db->like('product.title',$keyword);
			}
			/*
			if($date_from != '' && $date_to != '')
			{
				$this->db->where(
			}*/
			$this->db->order_by('product.created_time','DESC');
			$query  = $this->db->get();
			$result = $query->result_array();
			$query->free_result();
			
			if(!empty($result))
			{	
				return $result;
			}else{
				return false;
			}
		}
		
		public function _getParentMenu()
		{
			$this->db->select('id,parent_id,name,slug,order,link');
			$this->db->where('status',1);
			$this->db->where('parent_id',0);
			$this->db->order_by('order','DESC');
			$query = $this->db->get('no_frontend_menu');
			$result = $query->result_array();
			$query->free_result();
			
			return $result;
		}
		
		public function _getChildMenu($parent_id)
		{
			$this->db->select('id,parent_id,name,slug,order,link');
			$this->db->where('parent_id',$parent_id);
			$this->db->where('status',1);
			$this->db->order_by('order','DESC');
			$query  = $this->db->get('no_frontend_menu');
			$result = $query->result_array();
			$query->free_result();
			
			return $result;
		}
		
		public function getParentMenu()
		{
			$this->db->select('id,name');
			$this->db->where('parent_id', 0);
			$this->db->order_by('slug','DESC');
			$query = $this->db->get('no_frontend_menu');
			$result = $query->result_array();
			$query->free_result();
			
			return $result;
		}
		
		public function getSubMenu()
		{
			$this->db->select('*');
			$this->db->where('parent_id <> 0');
			$this->db->order_by('order','DESC');
			$query = $this->db->get('no_frontend_menu');
			
			$result = $query->result_array();
			$query->free_result();
			if(!empty($result))
			{
				return $result;
			}else{
				return false;
			}
		}
		
		/*
		*	
		*	Load bài viết với start và limit
		*
		*/
		public function admin_news($start = 0,$limit = 10)
		{
			$this->db->select('*');
			$this->db->order_by('order','DESC');
			$this->db->where('id','2');
			$this->db->limit($start,$limit);
			$query = $this->db->get($this->table);
			
			$result = $query->result_array();
			$query->free_result();
			if(!empty($result))
			{
				return $result;
			}else{
				return false;
			}
		}
		
		public function getParentCat()
		{
			$this->db->select('id,name');
			$this->db->where('parent_id',0);
			$this->db->order_by('slug','DESC');
			$query = $this->db->get('no_frontend_menu');
			$result = $query->result_array();
			$query->free_result();
			
			return $result;
		}
		
		public function getSubCat($id = 0)
		{
			$this->db->select('*');
			$this->db->where('parent_id',$id);
			$this->db->order_by('order','DESC');
			//$this->db->limit($start,$limit);
			$query = $this->db->get($this->table);
			
			$result = $query->result_array();
			$query->free_result();
			if(!empty($result))
			{
				return $result;
			}else{
				return false;
			}
		}
		
		
		/* FRONTEND */
		public function get_news($parent_id = 5,$limit = 5,$start = 0)
		{
			$this->db->select('*,name AS menu_name,slug AS slug_name');
			$this->db->where('parent_id',$parent_id);
			$this->db->where('status',1);
			$this->db->order_by('order','DESC');
			$this->db->limit($limit,$start);
			$query = $this->db->get('no_frontend_menu');
			$result = $query->result_array();
			$query->free_result();
			
			return $result;
		}
		
		public function get_sub_news($parent_id = 9,$limit = 5,$start = 0)
		{
			$this->db->select('*,name AS menu_name,slug AS slug_name');
			$this->db->where('id',$parent_id);
			$this->db->where('status',1);
			$this->db->order_by('order','DESC');
			$this->db->limit($limit,$start);
			$query = $this->db->get('no_frontend_menu');
			$result = $query->result_array();
			$query->free_result();
			
			return $result;
		}
		
		public function getNews($parent_id)
		{
			$this->db->select('*');
			$this->db->where('parent_id',$parent_id);
			$this->db->limit(1);
			$query  = $this->db->get($this->table);
			$result = $query->result_array();
			$query->free_result();
			return $result;
		}
		
		public function _getRelatedNews($id = 0, $parent_id = 0)
		{
			$this->db->select('*');
			$this->db->where('status',1);
			$this->db->where('parent_id',$parent_id);
			$this->db->where("id <> {$id}");
			$this->db->order_by('created_time');
			$this->db->limit(3);
			$query = $this->db->get($this->table);
			$result = $query->result_array();
			$query->free_result();
			return $result;
		}
		
		function get_image_product($id = 0)
		{
			$id = intval($id);
			$this->db->select('thumb');
			$this->db->where('id',$id);
			$query = $this->db->get($this->table);
			
			$result = $query->result_array();
			$query->free_result();
			
			if(!empty($result))
			{	
				return $result;
			}else{
				return false;
			}
		}
		
		public function get_images_library()
		{
			$this->db->select('*');
			$this->db->where('parent_id <> 0');
			$this->db->order_by('order','DESC');
			$this->db->limit(3);
			$query = $this->db->get('no_categories');
			$result = $query->result_array();
			$query->free_result();
			return $result;
		}
		
		public function getContent($id = 0,$limit = 0,$start = 0)
		{
			$this->db->select('news.*,menu.name AS menu_name,menu.slug AS menu_slug');
			$this->db->from('no_news AS news');
			$this->db->join('no_frontend_menu AS menu',"menu.id = news.parent_id",'LEFT');
			$this->db->where('news.parent_id',$id);
			$this->db->where('news.status',1);
			$this->db->limit($start,$limit);
			$this->db->order_by('news.created_time','DESC');
			$query = $this->db->get();
			$result = $query->result_array();
			$query->free_result();
			return $result;
		}
	
		public function getContentId($slug = '')
		{
			$this->db->select('news.*,menu.name AS menu_name, menu.slug  AS menu_slug');
			$this->db->from('no_news AS news');
			$this->db->join('no_frontend_menu AS menu','menu.id = news.parent_id','LEFT');
			$this->db->where('news.slug',$slug);
			$this->db->limit(1);
			$query = $this->db->get($this->table);
			$result = $query->result_array();
			$query->free_result();
			return $result;
		}
		
		public function relatedNews($slug = '')
		{
			$this->db->select('news.*,menu.name AS menu_name, menu.slug  AS menu_slug');
			$this->db->from('no_news AS news');
			$this->db->join('no_frontend_menu AS menu','menu.id = news.parent_id','LEFT');
			$this->db->where("news.id <> '$slug'");
			$this->db->limit(5);
			$query = $this->db->get($this->table);
			$result = $query->result_array();
			$query->free_result();
			return $result;
		}
		
		public function getParentContent($parent = 0,$start=0,$limit=2)
		{
			$this->db->select('menu.name AS menu_name,menu.slug AS menu_slug,news.*');
			$this->db->from('no_frontend_menu AS menu');
			$this->db->join('no_news AS news','news.parent_id = menu.id','LEFT');
			$this->db->where('menu.parent_id',$parent);
			$this->db->order_by('menu.slug','DESC');
			$this->db->limit($limit,$start);
			$query = $this->db->get();
			$result = $query->result_array();
			$query->free_result();
			return $result;
			
		}
		public function getAllContent($parent_id = 0)
		{
			$this->db->select('*');
			$this->db->where('parent_id',$parent_id );
			$this->db->order_by('slug','DESC');

			
			$query  = $this->db->get($this->table);
			$result = $query->result_array();
			$query->free_result();
			return $result;
		}
		
		public function count_content($id = 0)
		{
			$this->db->select('*');
			$this->db->where('parent_id',$id);
			$query = $this->db->get('no_news');
			
			$result = $query->result_array();
			$query->free_result();
			
			if(!empty($result))
			{
				return count($result);
			}else{
				return false;
			}
		}	
		
		public function count($id = 0)
		{
			$this->db->select('menu.*,news.*');
			$this->db->from('no_frontend_menu AS menu');
			$this->db->join('no_news AS news','news.parent_id = menu.id','LEFT');
			$this->db->where('menu.parent_id',$id);
			$this->db->order_by('menu.slug','DESC');
			$query = $this->db->get();
			$result = $query->result_array();
			$query->free_result();
			return count($result);
		}	
		
		public function getModule($id = 0)
		{
			$this->db->select('name,slug');
			$this->db->where('id',$id);
			$query = $this->db->get('no_frontend_menu');
			$result = $query->result_array();
			$query->free_result();
			return $result;
		}
		
		public function specAllContent($parent = 0)
		{
			$this->db->select('menu.name AS menu_name,news.*');
			$this->db->from('no_frontend_menu AS menu');
			$this->db->join('no_news AS news','news.parent_id = menu.id','LEFT');
			$this->db->where('menu.id',$parent);
			$this->db->order_by('menu.slug','DESC');
			$query = $this->db->get();
			$result = $query->result_array();
			$query->free_result();
			return $result;
		}
		
		public function count_hinhanh()
		{
			$this->db->select('*');
			$this->db->where('parent_id = 0');
			$query = $this->db->get('no_categories');
			$result = $query->result_array();
			$query->free_result();
			return count($result);
		}
		
		public function gethinhanh($start = 0, $limit = 6)
		{
			$this->db->select('*');
			$this->db->order_by('created_time');
			$this->db->where('parent_id',0);
			$this->db->limit($limit,$start);
			$query = $this->db->get('no_categories');
			$result = $query->result_array();
			$query->free_result();
			return $result;
		}
		
		public function getHinhAnhDetail($id = 0,$start = 0, $limit = 6)
		{
			$this->db->select('*');
			$this->db->where('parent_id',$id);
			$this->db->order_by('order','DESC');
			$this->db->limit($limit,$start);
			$query = $this->db->get('no_categories');
			$result = $query->result_array();
			$query->free_result();
			return $result;
		}
		public function hinhanhid($id = 0)
		{
			$this->db->select('*');
			$this->db->where('parent_id',$id);
			$this->db->order_by('order','DESC');
			$query = $this->db->get('no_images_detail');
			$result = $query->result_array();
			$query->free_result();
			return $result;
		}
		public function count_hinhanh_detail($id =0)
		{
			$this->db->select('*');
			$this->db->where('parent_id',$id);
			$query = $this->db->get('no_categories');
			$result = $query->result_array();
			$query->free_result();
			return count($result);
		}
		
		public function getSubModule($id = 0)
		{
			$this->db->select('name');
			$this->db->where('id',$id);
			$query = $this->db->get('no_frontend_menu');
			$result = $query->result_array();
			$query->free_result();
			return $result;
		}
		
		public function postlienhe()
		{
			$data = array('name' => $this->input->post('name'),
						'email' => $this->input->post('email'),
						'subject' => $this->input->post('subject'),
						'message' => $this->input->post('body'),
						'created_time' => getNow(),
						'modified_time' => getNow()
						);
						
			if($this->db->insert('no_contact',$data))
			{
				return true;
			}
			return false;
		}
	}
?>